package com.chezai.client.widget;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.CountDownTimer;
import android.view.Gravity;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chezai.client.R;
import com.chezai.client.global.Const;
import com.orhanobut.logger.Logger;

import butterknife.BindView;

/**
 * 二维码弹窗
 * Created by likunyang on 2017/8/4.
 */

public class QRDialog extends BaseDialog implements DialogInterface.OnShowListener, DialogInterface.OnDismissListener {
    @BindView(R.id.ivQR)
    ImageView ivQR;
    @BindView(R.id.tvCount)
    TextView tvCount;
    private CountDownTimer mCountDownTimer;
    private OnCountDownTimerFinishListener countDownTimerListener;

    public QRDialog(Context context) {
        super(context);
    }


    @Override
    protected int getDialogLayoutResId() {
        return R.layout.dailog_qrcode;
    }

    @Override
    protected void initView(Context context) {

    }

    @Override
    protected float getwScale() {
        return 0.7f;
    }

    @Override
    protected float gethScale() {
        return 0.9f;
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected void setListener() {
        setOnShowListener(this);
        setOnDismissListener(this);
    }

    /**
     * 设置二维码
     *
     * @param filePath 二维码文件路径
     */
    public void setQrBitmap(String filePath) {
        Glide.with(mContext).load(filePath).into(ivQR);
    }

    /**
     * 倒计时监听
     *
     * @param listener
     */
    public void setOnCountDownTimerFinishListener(OnCountDownTimerFinishListener listener) {
        this.countDownTimerListener = listener;
    }

    @Override
    public void onShow(DialogInterface dialogInterface) {
        Logger.t(TAG).i("QRDialog show");
        tvCount.setText(Const.QR_CODE_TIME + "s后自动刷新");
        mCountDownTimer = new CountDownTimer(Const.QR_CODE_TIME * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                tvCount.setText(millisUntilFinished / 1000 + "s后自动刷新");
            }

            @Override
            public void onFinish() {
                if (isShowing()) {
                    if (mContext instanceof Activity) {
                        if (!((Activity) mContext).isFinishing() && !((Activity) mContext).isDestroyed())
                            dismiss();
                    } else {
                        dismiss();
                    }
                }
                if (countDownTimerListener != null) {
                    countDownTimerListener.onCountDownTimerFinish(this);
                }
            }
        }.start();
    }

    @Override
    public void onDismiss(DialogInterface dialogInterface) {
        Logger.t(TAG).i("QRDialog dismiss");
        if (null != mCountDownTimer) {
            mCountDownTimer.cancel();
            mCountDownTimer = null;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && isShowing()) {
            dismiss();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public interface OnCountDownTimerFinishListener {
        /**
         * ，倒计时完成时回调
         *
         * @param countDownTimer 计时任务
         */
        void onCountDownTimerFinish(CountDownTimer countDownTimer);
    }
}
